﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;

namespace Orchard.Test.Library
{
    public class TagHelper:UIHelperBase
    {
        public TagHelper(TestLibrary settings) : base(settings) { }

        private int GetTagIndexOnManageTagsPage(Tag tag)
        {
            var count = (int)selenium.GetXpathCount(TagsLocator);
            var index = 1;
            for (; index <= count; ++index)
            {
                string path = String.Format("{0}[{1}]/td[2]/a", TagsLocator, index);
                var text = selenium.GetText(path);
                if (String.CompareOrdinal(tag.Name, text) == 0)
                    break;
            }

            if (index > count)
                throw new CaseFailureException(String.Format("Can't find tag {0} in Manage Tags page!", tag.Name));

            return index;
        }

        public void ManageTags()
        {
            if (!selenium.GetLocation().EndsWith("/Admin/Tags"))
            {
                selenium.Open("/Admin/Tags");
            }
        }

        public bool IsTagPresent(Tag tag)
        {
            ManageTags();
            return selenium.IsElementPresent(String.Format("link={0}", tag.Name));
        }   

        public int GetTagCount()
        {
            ManageTags();
            return (int)selenium.GetXpathCount(TagsLocator);
        }

        public Tag CreateTag(string name)
        {
            ManageTags();
            selenium.Type("TagName",name);
            ClickAndWait("//button[@name='submit.Create']");
            return new Tag(name, null);
        }

        public void NavigateToEditTag(Tag tag)
        {
            ManageTags();
            int index = GetTagIndexOnManageTagsPage(tag);
            ClickAndWait(String.Format("{0}[{1}]/td[3]/a", TagsLocator, index)); 
        }

        public void NavigateToEditTagByTitleLink(Tag tag)
        {
            ManageTags();
            int index = GetTagIndexOnManageTagsPage(tag);
            ClickAndWait(String.Format("{0}[{1}]/td[2]/a", TagsLocator, index));
        }

        public Tag EditTag(Tag tag, string newTagName)
        {
            NavigateToEditTag(tag);
            selenium.Type("TagName", newTagName);
            Submit();
            return new Tag(newTagName, tag.ContentItems);
        }

        public Tag EditTagByTitleLink(Tag tag, string newTagName)
        {
            NavigateToEditTagByTitleLink(tag);
            selenium.Type("TagName", newTagName);
            Submit();
            return new Tag(newTagName, tag.ContentItems);
        }

        public void SelectDeleteTag(Tag tag)
        {
            ManageTags();
            int index = GetTagIndexOnManageTagsPage(tag);
            selenium.Check(String.Format("{0}[{1}]/td[1]/input[@type='checkbox']", TagsLocator, index));
            selenium.Select("publishActions", "label=Delete");
            ClickAndWait("//button[@name='submit.BulkEdit']");
        }

        public void DeleteTag(Tag tag)
        {
            ManageTags();
            int index = GetTagIndexOnManageTagsPage(tag);
            ClickAndWait(String.Format("{0}[{1}]/td[3]/a[2]", TagsLocator, index));
        }
    
        public void NavigateToContentsByTag(Tag tag)
        {
            ManageTags();
            int index = GetTagIndexOnManageTagsPage(tag);
            ClickAndWait(String.Format("{0}[{1}]/td[2]/a", TagsLocator, index));            
        }
    }

    public class Tag
    {
        public string Name { set; get; }

        public List<IContent> ContentItems { set; get; }

        public Tag(string name, List<IContent> contentItems)
        {
            Name = name;
            ContentItems = new List<IContent>();
            if (contentItems != null)
            {
                foreach (IContent contentItem in contentItems)
                {
                    ContentItems.Add(contentItem);
                }
            }
        }
    }
}
